public class FruitCollection {
    private Fruit[] fruits = new Fruit[5];
    private int size = 0;

    public void add(Fruit fruit) {
        fruits[size++] = fruit;
    }

    public FruitIterator iterator() {
        return new FruitArrayIterator(fruits);
    }

    private class FruitArrayIterator implements FruitIterator {
        private int index = 0;
        private Fruit[] fruits;

        public FruitArrayIterator(Fruit[] fruits) {
            this.fruits = fruits;
        }

        @Override
        public boolean hasNext() {
            return index < fruits.length && fruits[index] != null;
        }

        @Override
        public Fruit next() {
            return fruits[index++];
        }
    }
}
